import Foundation
import Moya

//极光推送相关api
enum JPushApi {
    case getExample
    case postFormExample(formData: [String: Any])
    case pushJg(token:String, jsonString: String)
}

extension JPushApi: TargetType {
    var baseURL: URL {
        return URL(string: "https://api.jpush.cn")!
    }
    
    var path: String {
        switch self {
        case .getExample:
            return "/get-example"
        case .postFormExample, .pushJg:
            return "/v3/push" 
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getExample:
            return .get
        case .postFormExample, .pushJg:
            return .post
        }
    }
    
    var task: Task {
        switch self {
        case .getExample:
            return .requestParameters(parameters: [:], encoding: JSONEncoding.default)  // GET 请求通常不需要编码参数
        case .postFormExample(let formData):
            return .requestParameters(parameters: formData, encoding: URLEncoding.default)
        case .pushJg(_, let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                      return .requestData(jsonData)
                  }
                  return .requestPlain
//            if let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: []) {
//                return .requestData(jsonData)
//            } else {
//                // 处理 JSON 序列化失败的情况
//                fatalError("Unable to serialize JSON object for POST request")
//            }
        }
    }
    
    var headers: [String: String]? {
        var headers: [String: String] = ["Content-Type": "application/json"]
        
        switch self {
        case .pushJg(let token, _):
            headers["Authorization"] = "Basic \(token)"
            return headers
        default : return headers
        }
    }
    
    private var sampleData: Data? {
        // 通常用于模拟网络请求的响应数据
        return nil
    }
    
    var validate: Bool {
        return true  // 根据需要开启或关闭响应验证
    }
}
